<template>
  <div>
    <el-dialog
      title="编辑用户"
      :visible="editShow"
      width="50%"
      @close="onEditDialogClose"
    >
      <el-form ref="Form" :model="editForm" status-icon :rules="rules" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名">
          <el-input v-model="editForm.username" :disabled="true" />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="editForm.email" />
        </el-form-item>
        <el-form-item label="手机" prop="mobile">
          <el-input v-model="editForm.mobile" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('input',false)">取 消</el-button>
        <el-button type="primary" @click="editBtn">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getUserInfoById, editSubmitUser } from '@/api/user.js'

export default {
  model: {
    prop: 'editShow'
  },
  props: {
    editShow: {
      type: Boolean,
      required: true
    }
  },
  data() {
    return {
      editForm: {
        username: '',
        email: '',
        mobile: ''
      },
      rules: {
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validateField: '/^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/', message: '请输入正确格式', trigger: 'blur' }
        ]
      }
    }
  },

  created() {

  },

  methods: {
    onEditDialogClose() {
      this.$emit('input', false)
    },
    async editUser(id) {
      const res = await getUserInfoById(id)
      this.editForm = res
    },
    async editBtn() {
      await this.$refs.Form.validate()
      try {
        await editSubmitUser(this.editForm)
        this.$message.success('更新成功')
        this.onEditDialogClose()
        this.$emit('loadUser')
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang='less'>

</style>
